package org.cometmi.gwt.echo1.client;

import org.cometmi.gwt.client.Connection;
import org.cometmi.gwt.echo1.shared.MyTestProxy;
import org.cometmi.gwt.shared.MiQueue;
import org.cometmi.gwt.shared.MiQueuePool;
import org.cometmi.gwt.shared.events.BindCallback;
import org.cometmi.gwt.shared.exceptions.InvalidArgumentException;
import org.cometmi.gwt.shared.exceptions.NameParseException;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

public class Echo1Test implements EntryPoint
{
	Connection conn = Connection.INSTANCE;

	@Override
	public void onModuleLoad()
	{
		try
		{
			MiQueue qListen = conn.getQueue("org.cometmi.gwt.test.echo1");
			final MiQueuePool qSend = conn.getQueue("org.cometmi.gwt.test.echo1");

			qListen.bind(new BindCallback()
			{
				@Override
				public void onSuccess()
				{
					try
					{
						init(qSend);
					}
					catch (InvalidArgumentException e)
					{}
				}

				@Override
				public void onError(Throwable t)
				{
					Window.alert(t.getMessage());
				}
			});
		}
		catch (NameParseException e)
		{
			e.printStackTrace();
		}
	}

	void init(MiQueuePool qSend) throws InvalidArgumentException
	{
		final MyTestProxy proxy = qSend.proxyToNewObject(MyTestProxy.class, "costr str");

		final TextBox echoBox = new TextBox();

		Button sendButton = new Button("Send", new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				try
				{
					proxy.myMethod1(echoBox.getValue());
				}
				catch (InvalidArgumentException e)
				{
					e.printStackTrace();
				}
			}
		});

		RootPanel.get().add(echoBox);
		RootPanel.get().add(sendButton);
	}
}
